#include <iostream>
#include <unordered_map>
#include <vector>

// 定义地址长度为4bit
const int ADDRESS_LENGTH = 4;
// 定义广播地址
const int BROADCAST_ADDRESS = 0xF;

// 定义帧的结构体
struct Frame {
    int sourceAddress;
    int sourcePort;
    int destinationAddress;
};

// 模拟网桥的类
class Bridge {
private:
    std::unordered_map<int, int> macAddressTable;
public:
    // 处理帧的函数
    void processFrame(Frame frame) {
        // 学习源地址
        macAddressTable[frame.sourceAddress] = frame.sourcePort;

        // 判断目的地址是否为广播地址
        if (frame.destinationAddress == BROADCAST_ADDRESS) {
            std::cout << "目的端口: 广播" << std::endl;
        } else {
            // 查找目的地址对应的端口
            auto it = macAddressTable.find(frame.destinationAddress);
            if (it != macAddressTable.end()) {
                std::cout << "目的端口: " << it->second << std::endl;
            } else {
                std::cout << "目的端口: 泛洪" << std::endl;
            }
        }
    }

    // 打印MAC地址表的函数
    void printMACAddressTable() {
        std::cout << "MAC地址表:" << std::endl;
        for (const auto& entry : macAddressTable) {
            std::cout << "MAC地址: " << entry.first << " -> 端口: " << entry.second << std::endl;
        }
    }
};

int main() {
    Bridge bridge;
    std::vector<Frame> frames = {
        {0x1, 1, 0x2},
        {0x2, 2, 0x1},
        {0x3, 3, 0xF}
    };

    for (const auto& frame : frames) {
        bridge.processFrame(frame);
    }

    bridge.printMACAddressTable();

    return 0;
}